package com.example.music_project.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.music_project.R;
import com.example.music_project.adapter.MusicAdapter;
import com.example.music_project.entity.Music;
import com.example.music_project.pages.MusicPlayMainActivity;

import java.util.ArrayList;
import java.util.List;

public class MusicFragment extends Fragment {
    private ActivityResultLauncher<Intent> musicPlayLauncher;
    private List<Music> musicData = new ArrayList<>();
    private TextView favouriteText; // 声明为成员变量

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_music, container, false);

        // 初始化音乐数据
        initData();

        // 设置 ListView
        ListView musicList = view.findViewById(R.id.list_music);
        MusicAdapter adapter = new MusicAdapter(getContext(), R.layout.item_music, musicData);
        musicList.setAdapter(adapter);

        // 点击事件
        musicList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Music selectedMusic = musicData.get(position);
                Intent intent = new Intent(getContext(), MusicPlayMainActivity.class);
                intent.putExtra("musicName", selectedMusic.getName());
                intent.putExtra("musicImg", selectedMusic.getImg());
                intent.putExtra("musicLyric", selectedMusic.getLyric());
                // 使用 ActivityResultLauncher 启动 Activity
                musicPlayLauncher.launch(intent);
            }
        });

        return view;
    }

    /*加载音乐数据*/
    private void initData(){

        // 起风了
        Music theWindRises = new Music(R.drawable.the_wind_rises,"起风了","高桥优",
                "這一路上走走停停\n" +
                        "順著少年漂流的痕跡\n" +
                        "邁出車站的前一刻\n" +
                        "竟有些猶豫\n" +
                        "不禁笑這近鄉情怯\n" +
                        "仍無法避免\n" +
                        "而長野的天 依舊那麼暖\n" +
                        "風吹起了從前\n" +
                        "從前初識這世間 萬般流連\n" +
                        "看著天邊似在眼前\n" +
                        "也甘願赴湯蹈火去走它一遍\n" +
                        "如今走過這世間 萬般流連\n" +
                        "翻過歲月不同側臉\n" +
                        "措不及防闖入你的笑顏\n" +
                        "......");
        // 大海
        Music sea = new Music(R.drawable.sea,"大海","张雨生",
                "从那遥远海边\n" +
                        "慢慢消失的你\n" +
                        "本来模糊的脸\n" +
                        "竟然渐渐清晰\n" +
                        "想要说些什么\n" +
                        "又不知从何说起\n" +
                        "只有把它放在心底\n" +
                        "茫然走在海边\n" +
                        "看那潮来潮去\n" +
                        "徒劳无功\n" +
                        "想把每朵浪花记清\n" +
                        "想要说声爱你\n" +
                        "却被吹散在风里\n" +
                        "猛然回头你在那里\n" +
                        "如果大海能够唤回曾经的爱\n" +
                        "就让我用一生等待\n" +
                        "如果深情往事你已不再留恋\n" +
                        "就让它随风飘远\n" +
                        "如果大海能够带走我的哀愁\n" +
                        "就像带走每条河流\n" +
                        "所有受过的伤\n" +
                        "所有流过的泪\n" +
                        "我的爱\n" +
                        "请全部带走\n" +
                        "......");

        // 晴天
        Music clearDay = new Music(R.drawable.clear_day,"晴天","周杰伦",
                "故事的小黃花\n" +
                        "從出生那年就飄著\n" +
                        "童年的盪鞦韆\n" +
                        "隨記憶一直晃到現在\n" +
                        "Re-So-So-Si-Do-Si-La\n" +
                        "So-La-Si-Si-Si-Si, La-Si-La-So\n" +
                        "吹著前奏 望著天空\n" +
                        "我想起花瓣試著掉落\n" +
                        "為你翹課的那一天 教室的那一間\n" +
                        "下雨的那一天 我怎麼看不見?\n" +
                        "消失的下雨天 我好想再淋一遍\n" +
                        "沒想到失去的勇氣我還留著\n" +
                        "好想再問一遍 你會等待還是離開?\n" +
                        "颳風這天 我試過握著你手\n" +
                        "但偏偏雨漸漸大到我看你不見\n" +
                        "還要多久 我才能在你身邊?\n" +
                        "等到放晴的那天 也許我會比較好一點\n"+
                        "......");

        // 再见
        Music bai = new Music(R.drawable.baibai,"再见","张震岳",
                "我怕我没有机会\n" +
                        "跟你说一声再见\n" +
                        "因为也许就再也见不到你\n" +
                        "明天我要离开\n" +
                        "熟悉的地方和你\n" +
                        "要分离\n" +
                        "我眼泪就掉下去\n" +
                        "我会牢牢记住你的脸\n" +
                        "我会珍惜你给的思念\n" +
                        "这些日子在我心中永远都不会抹去\n" +
                        "我不能答应你\n" +
                        "我是否会再回来\n" +
                        "不回头\n" +
                        "不回头地走下去\n" +
                        "我怕我没有机会\n" +
                        "跟你说一声再见\n" +
                        "因为也许就再也见不到你\n" +
                        "明天我要离开\n" +
                        "熟悉的地方和你\n" +
                        "要分离\n" +
                        "我眼泪就掉下去\n"+
                        "......");

        // 天外来物
        Music tian = new Music(R.drawable.tian,"天外来物","薛之谦",
                "你降落的 太突然了\n" +
                        "我刚好呢 又路过了\n" +
                        "机会难得 又主观觉得\n" +
                        "想明抢 又碰不得\n" +
                        "你带来了 我的快乐\n" +
                        "让这世界 有点颜色\n" +
                        "我好想指责 你太随意了\n" +
                        "宝物该有人捧着 你是不是我的\n" +
                        "你像 天外来物一样 求之不得\n" +
                        "你在世俗里的名字 不重要了\n" +
                        "正好 我隐藏的人格是契而不舍\n" +
                        "直到蜂拥而至的人都透明了\n" +
                        "我在 不近又不远处\n" +
                        "用明天换你 靠近我\n" +
                        "你占领了 我的快乐\n" +
                        "和这世界 已没有瓜葛\n" +
                        "任事物干渴 都褪去颜色\n" +
                        "只有你是天蓝色 我开始找你了\n"+"......");

        // 私奔
        Music siBen = new Music(R.drawable.siben,"私奔","郑钧",
                "把青春献给身后那座辉煌的都市\n" +
                        "为了这个美梦我们付出着代价\n" +
                        "把爱情留给我身边最真心的姑娘\n" +
                        "你陪我歌唱你陪我流浪陪我两败俱伤\n" +
                        "一直到现在才突然明白我梦寐以求\n" +
                        "是真爱和自由\n" +
                        "想带上你私奔\n" +
                        "奔向最遥远城镇\n" +
                        "想带上你私奔\n" +
                        "去做最幸福的人\n" +
                        "在熟悉的异乡我将自己一年年流放\n" +
                        "穿过鲜花走过荆棘只为自由之地\n" +
                        "在欲望的城市你就是我最后的信仰\n" +
                        "洁白如一道喜乐的光芒将我心照亮\n" +
                        "不要再悲伤\n" +
                        "我看到了希望\n" +
                        "你是否还有勇气 随着我离去\n" +
                        "想带上你私奔\n" +
                        "奔向最遥远城镇\n" +
                        "想带上你私奔\n"+"......");
        musicData.add(theWindRises);
        musicData.add(sea);
        musicData.add(clearDay);
        musicData.add(bai);
        musicData.add(tian);
        musicData.add(siBen);

    }



    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 通过 view.findViewById() 查找 TextView
        favouriteText = view.findViewById(R.id.text_favourite);

        // 初始化 ActivityResultLauncher
        musicPlayLauncher = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            String favouriteName = data.getStringExtra("favouriteName");
                            // 直接使用已初始化的 favouriteText
                            favouriteText.setText("当前播放: " + favouriteName);
                            favouriteText.setTextColor(Color.rgb(255, 255, 240));
                            favouriteText.setShadowLayer(3, 2, 2, Color.BLACK);
                        }
                    }
                }
        );
    }
}